/* Copyright 2009 Requirements Management System 
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.mags.remas.view.widgets;

import org.eclipse.jface.viewers.DialogCellEditor;  
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.mags.common.EngineContext;
import org.mags.remas.control.commons.ControlUtils;
import org.mags.remas.exception.SingletonExceptionHandler;
import org.mags.remas.model.entity.EntityCategory;
import org.mags.remas.model.entity.EntityRelationship;
import org.mags.remas.model.entity.EntityStructure;
import org.mags.remas.view.dialogs.RelationshipTreeDialog;
import org.mags.remas.view.dialogs.SingletonRelationshipTreeDialog;

/**
 * 
 * @author Mario Gaitan
 *
 */
public class ButtonLinkCellEditor extends DialogCellEditor {
	public ButtonLinkCellEditor(Composite parent, int style) {
        super(parent, style);
    }	
	protected Object openDialogBox(Control cellEditorWindow) {
		if(super.getValue()==null)
			return null;
		EntityRelationship entityRelationship = (EntityRelationship)super.getValue();
		EntityStructure entityStructure = EngineContext.getDataContext().getEntityProviderFactory().getEntityStructureProvider().getById(entityRelationship.getEntity().getEntityType().getEntityTypeId(), entityRelationship.getEntityParent().getEntityType().getEntityTypeId());
		if(entityStructure.getLinkStructures().size()>0) {
			SingletonRelationshipTreeDialog treeDialog = SingletonRelationshipTreeDialog.getInstance(super.getControl().getShell().getDisplay());
			if(entityRelationship.getLink()!=null)
				treeDialog.setSelectedEntityRelationship(entityRelationship.getLink());
			else {
				EntityCategory entityCategory = ControlUtils.selectCategory(entityStructure);
				if(entityCategory!=null)
					treeDialog.setSelectedEntityCategory(entityCategory);						
			}
			String text = ControlUtils.makeSelectLabel(entityStructure);
			text = "select: "+text;		
			treeDialog.setText(text);
			treeDialog.show();
			if(treeDialog.getStatus()==RelationshipTreeDialog.STATUS_CANCELED)
				return entityRelationship;
			EntityRelationship newLink = treeDialog.getSelectedEntityRelationship();
			if(entityStructure.getNullLink().equalsIgnoreCase("N")&&newLink==null) {
				SingletonExceptionHandler.getInstance().showError("link can't be null");
				return entityRelationship;
			}				
			entityRelationship.setLink(newLink);
		}
		return entityRelationship;
	}
    protected void updateContents(Object value) {
        if (super.getDefaultLabel() == null) {
			return;
		}
        String text = "";
        if (value != null) {
        	EntityRelationship link = (EntityRelationship)value;
        	if(link.getLink()!=null)
        		text = link.getLink().getEntity().getCode()+")"+link.getLink().getEntity().getName();
		}
        super.getDefaultLabel().setText(text);
    }	
}
